﻿using EagleProject.LinQDataContext;
using EagleProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EagleProject.Controllers
{
    public class UserController : MyController
    {
        private void UserLogin(CommonUser u)
        {
            UserInformation infor = new UserInformation
            {
                ID = u.ID,
                UserName = u.Username,
                UserType = u.UserType.ToLower()
            };
            SetUserInformation(infor);
        }

        //
        // GET: /User/ControlPanel
        public ActionResult ControlPanel()
        {
            if (!IsAuthenticated()) return RedirectToHome();

            return View();
        }

        //
        // GET: /User/Success
        public ActionResult Success()
        {
            if (!IsAuthenticated())
            {
                return RedirectToHome();
            }
            return View();
        }

        //
        // GET: /User/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /User/Login/
        public ActionResult Login()
        {
            // TODO: Bỏ sau
            //if (IsAuthenticated()) return RedirectToHome();

            string returnUrl = GetStringParam("ReturnUrl");
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /User/Login/
        [HttpPost]
        public ActionResult Login(UserLoginModel model, string returnUrl)
        {
            // TODO: bỏ sau
            //if (IsAuthenticated) { return RedirectToHome(); }

            CommonUser u = DatabaseController.GetCommonUser(model.UserName, model.Password);
            if (u != null)
            {
                UserLogin(u);
                // Success
                if (string.IsNullOrEmpty(returnUrl)) { return RedirectToAction("Index", "Home"); }
                else { return Redirect(returnUrl); }
            }
            else
            {
                // User name or password wrong?
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
                return View(model);
            }
        }

        // GET: /User/LogOut
        public ActionResult LogOut()
        {
            SetUserInformation(null);
            return RedirectToHome();
        }

        public ActionResult ChangeEmail()
        {
            return View();
        }

        // GET: /User/ChangePassword
        public ActionResult ChangePassword()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/User/ChangePassword");

            return View();
        }

        // POST: /User/ChangePassword
        [HttpPost]
        public ActionResult ChangePassword(UserPasswordChangeModel model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/User/ChangePassword");

            bool isDone = DatabaseController.UserChangePassword(GetUserInformation().ID,
                model.Password, model.NewPassword);
            if (isDone)
            {
                return RedirectToAction("Success");
            }
            else
            {
                // Wrong Password
                ModelState.AddModelError("", "Wrong password !");
                return View(model);
            }
        }

        // GET: /User/ChangeProfile
        public ActionResult ChangeProfile()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/User/ChangeProfile");

            CommonUser u = DatabaseController.GetCommonUser(GetUserInformation().ID);

            return View(u);
        }

        // POST: /User/ChangeProfile
        [HttpPost]
        public ActionResult ChangeProfile(CommonUser model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/User/ChangeProfile");

            bool isUpFile = IsUploadFile();

            model.ID = GetUserInformation().ID;

            if (isUpFile)
            {
                System.IO.File.Delete(Server.MapPath(GetUserInformation().UserAvatar));
                string url = HandleFileUpload("~/Upload/Avatar");
                model.PictureUrl = url;
                GetUserInformation().UserAvatar = url;
            }

            DatabaseController.EditUserProfile(model);

            return RedirectToAction("Success");
        }

        // GET: /User/UserProfile
        public ActionResult UserProfile()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/User/Profile");

            CommonUser u = DatabaseController.GetCommonUser(GetUserInformation().ID);

            return View(u);
        }

        // GET: /User/Register
        public ActionResult Register()
        {
            if (IsAuthenticated()) return RedirectToHome();
            return View();
        }

        // POST: /User/Register
        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (IsAuthenticated()) return RedirectToHome();

            var user = DatabaseController.CreateSubscriberCommonUser(model);
            if (user != null)
            {
                UserLogin(user);
                return RedirectToAction("Success");
            }
            else
            {
                ModelState.AddModelError("", "User name already existed !");
                return View(model);
            }
        }
    }
}
